﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

namespace StockMatch.Service
{
    public class JPowerPoint {
        public string PowerName { get; set; }
        public string PowerMark { get; set; }
        public string PowerCode { get; set; }

        public List<JPoint> Items { get; set; }
    }

    public class JPoint {
        public string PointName { get; set; }
        public string[] PointCode { get; set; }
        public string PointMark { get; set; }
    }
    public class RolePowerConfig
    {
        public RolePowerConfig()
        {
            List<JPowerPoint> JPowerPoints = GetPowerPoint();
            AOPConfig rolePowerConfig = new AOPConfig();
            foreach (var item in JPowerPoints)
            {
                foreach (var ms in item.Items)
                {
                    foreach (var m in ms.PointCode)
                    {
                        rolePowerConfig.AddMethodName(string.Format("QiPei.Service.{0}.{1}", item.PowerCode, m));
                    }
                }
            }
            rolePowerConfig.InterceptActhieveType = InterceptAchieveType.Before;
            rolePowerConfig.AOPInterceptAchieve = new RolePowerAchieve();
            ConfigRelationExecution.ConfigRelation.Add("RolePower", rolePowerConfig);
        }

        public List<JPowerPoint> GetPowerPoint() {
            try
            {
                using (StreamReader r = new StreamReader(AppDomain.CurrentDomain.BaseDirectory+"/PowerPoint.json"))
                {
                    string json = r.ReadToEnd();
                    //dynamic array = JsonConvert.DeserializeObject(json);
                    return JsonConvert.DeserializeObject<List<JPowerPoint>>(json);
                }
            }
            catch (Exception ex) {
                throw new Exception("权限文件配置错误：" + ex.Message);
            }
        }
    }
}